﻿using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Updating;
using DevExpress.Xpo;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.BaseImpl;
using Microsoft.Extensions.DependencyInjection;
using XafListViewDefaultNoLoadData.Module.BusinessObjects;

namespace XafListViewDefaultNoLoadData.Module.DatabaseUpdate;

// For more typical usage scenarios, be sure to check out https://docs.devexpress.com/eXpressAppFramework/DevExpress.ExpressApp.Updating.ModuleUpdater
public class Updater : ModuleUpdater {
  public Updater(IObjectSpace objectSpace, Version currentDBVersion) :
      base(objectSpace, currentDBVersion) {
  }
  public override void UpdateDatabaseAfterUpdateSchema() {
    base.UpdateDatabaseAfterUpdateSchema();
    string[] bNames = ["张三", "李四"];
    for (int i = 0; i < bNames.Length; i++) {
      string bName = bNames[i];
      TStudentBo bObject = ObjectSpace.FirstOrDefault<TStudentBo>(u => u.Name == bName);
      if (bObject == null) {
        bObject = ObjectSpace.CreateObject<TStudentBo>();
        bObject.Name = bName;
        bObject.Age = 20 + i;
      }
    }

    ObjectSpace.CommitChanges(); //Uncomment this line to persist created object(s).
  }
  public override void UpdateDatabaseBeforeUpdateSchema() {
    base.UpdateDatabaseBeforeUpdateSchema();
    //if(CurrentDBVersion < new Version("1.1.0.0") && CurrentDBVersion > new Version("0.0.0.0")) {
    //    RenameColumn("DomainObject1Table", "OldColumnName", "NewColumnName");
    //}
  }
}
